package com.liebaut.util;

import org.codehaus.jackson.map.ObjectMapper;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Map;

public class JsonHelper {
    private static ObjectMapper objectMapper;

    static {
        objectMapper  = new ObjectMapper();
//        objectMapper.configure(SerializationConfig.Feature.INDENT_OUTPUT, true);
    }

    public static Map parse(String string) {
        return parse(string, Map.class);
    }

    public static <T> T parse(String string, Class<T> clazz) {
        try {
            return objectMapper.readValue(string, clazz);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static String toJsonString(Object value) {
        try {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            objectMapper.writeValue(out, value);
            return new String(out.toByteArray());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}
